import axios from 'axios'
import { django_path } from '@/mylibs/define'
axios.defaults.baseURL = django_path
// axios.defaults.baseURL ='/api'

// 判断是否登录
export function is_signin(fun_back) {
  axios({ url: 'blogs/api/is_signin/', withCredentials: true, method: 'post' })
    .then((response) => {
      if (fun_back) {
        fun_back(response)
      }
    })
    .catch(function (error) {
      // 请求失败处理
      console.log(error)
    })
}

// 登录
export function signin(username, password, fun_back) {
  var upload = {
    username: username,
    password: password
  }
  axios({
    url: 'blogs/api/signin/',
    method: 'post',
    data: upload,
    // 影响sessionid 发送到django
    withCredentials: true // 允许携带cookie
  })
    .then((response) => {
      if (fun_back) {
        fun_back(response)
      }
    })
    .catch(function (error) {
      // 请求失败处理
      console.log(error)
    })
}

// 退出登录
export function signout(fun_back) {
  axios({
    url: 'blogs/api/signout/',
    method: 'post',
    withCredentials: true
  }).then((res) => {
    if (fun_back) {
      fun_back(res)
    }
  })
}
